from flask import Flask, request
from home import home
from blog import blog

app = Flask(__name__)


app.register_blueprint(home, url_prefix='/')
app.register_blueprint(blog, url_prefix='/blog')

#设置签名，对session 进行加密
app.secret_key='xxxxx'

# 设置默认的 Session 过期时间为 30 分钟
app.permanent_session_lifetime=30*60

# flask 3 已经移除此方法
#@app.before_first_request
def before_first_request():
    print('这个函数会在第一次请求之前执行一次')

@app.before_request
def before_request():
    print('请求前执行了的函数')

@app.after_request
def blog_after_request(response):
    print('this is app_after_request')
    # 添加自定义响应头
    response.headers['APP-Custom-Header'] = 'My Custom Header Value app'
    return response
@app.errorhandler(404)
def errorhandler(e):
    print('异常了',e)
    return '404 error'

@app.teardown_request
def teardown_request(exception=None):
    print("This will be executed after every request, even if an exception occurred.")
    # 这里可以进行一些清理工作，例如关闭数据库连接

if __name__ == '__main__':
    app.run(debug=True)
